package com.kfh.ybracelet.my;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.kfh.ybracelet.R;
import com.kfh.ybracelet.base.BaseActivity;
import com.kfh.ybracelet.location.SnCodeActivity;
import com.kfh.ybracelet.net.HttpUtil;
import com.kfh.ybracelet.utils.CommonUtils;
import com.kfh.ybracelet.utils.Constant;
import com.kfh.ybracelet.widget.ActionSheetDialog;
import com.kfh.ybracelet.widget.MyDatePicker;

import java.util.Calendar;

/**
 * 创建空间
 * Created by Administrator on 2015/8/13.
 */
public class CreateBabySpaceActivity extends BaseActivity {

    private EditText et_name;
    private EditText et_birthday;
    private EditText et_sex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_create_baby_space);
//        mApplication.addActivity(this);
        init();
    }

    private void init() {
        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText(getResources().getString(R.string.create_space));
        et_name = (EditText) findViewById(R.id.et_name);
        et_birthday = (EditText) findViewById(R.id.et_birthday);
        et_sex = (EditText) findViewById(R.id.et_sex);
        TextView tv_right = (TextView) findViewById(R.id.tv_right);
        tv_right.setText(getResources().getString(R.string.done));
        et_birthday.setOnClickListener(this);
        et_sex.setOnClickListener(this);
        iv_left.setOnClickListener(this);
        tv_right.setOnClickListener(this);
    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {

            case R.id.iv_left:
                finish();
                break;

            case R.id.tv_right:

                String name = et_name.getText().toString().trim();
                String sex = et_sex.getText().toString().trim();
                String birthday = et_birthday.getText().toString().trim();
                if (CommonUtils.isNull(name)) {
                    showToast("姓名不能为空");
                    return;
                }
                if (CommonUtils.isNull(sex)) {
                    showToast("性别不能为空");
                    return;
                }
                if (CommonUtils.isNull(birthday)) {
                    showToast("生日不能为空");
                    return;
                }

                createBabaySpace();

                break;

            case R.id.et_sex:
                showChoose();
                break;

            case R.id.et_birthday:

                Calendar c = Calendar.getInstance();

                new MyDatePicker(CreateBabySpaceActivity.this, new DatePickerDialog.OnDateSetListener() {

                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear,
                                          int dayOfMonth) {

                        String month = (monthOfYear + 1) < 10 ? "0" + (monthOfYear + 1) : (monthOfYear + 1) + "";
                        String day = dayOfMonth < 10 ? "0" + dayOfMonth : dayOfMonth + "";

                        et_birthday.setText(year + "-" + month + "-" + day);

                    }


                }, c.get(Calendar.YEAR), c.get(Calendar.MONTH),
                        c.get(Calendar.DAY_OF_MONTH)).show();
                break;
        }
    }

    /**
     * 弹出选择照片的对话框
     */
    @Deprecated
    protected void showChoose() {

        new ActionSheetDialog(this)
                .builder()
                .setCancelable(true)
                .setCanceledOnTouchOutside(true)
                .addSheetItem("男", ActionSheetDialog.SheetItemColor.Blue,
                        new ActionSheetDialog.OnSheetItemClickListener() {
                            @Override
                            public void onClick(int which) {
                                et_sex.setText("男");
                            }

                        })
                .addSheetItem("女", ActionSheetDialog.SheetItemColor.Blue,
                        new ActionSheetDialog.OnSheetItemClickListener() {
                            @Override
                            public void onClick(int which) {
                                et_sex.setText("女");
                            }
                        }).show();

    }

    /**
     * 创建宝宝空间
     */
    private void createBabaySpace() {

//        final CreateBabySpaceBean createBabySpaceBean = new CreateBabySpaceBean();

        JSONObject j = new JSONObject();
        j.put("token", sp.getString(Constant.TOKEN, ""));
        j.put("birthday", et_birthday.getText().toString().trim());
        j.put("name", et_name.getText().toString().trim());
        j.put("sex", et_sex.getText().toString().trim());

        HttpUtil.setPostRequest("创建中...", this, Constant.WEB_ROOT + Constant.CREATECHILD,
                j.toJSONString(), getClass().getName(), new HttpUtil.SuccessListener() {

                    @Override
                    public void setOnSuccessListener(String str) {
                        try {

                            org.json.JSONObject result = new org.json.JSONObject(str);

//                            if (result.getInt("code") == Constant.STATUS_SUCCESS) {

                                showToast("创建成功!");

                                //把当前添加的老人 设置默认的老人
                                sp.edit().putString(Constant.CURRENTCHILDID, result.getString("id")).apply();

                                //跳到输入IMEI码 的页面
                                Intent mIntent = new Intent(CreateBabySpaceActivity.this, SnCodeActivity.class);
                                mIntent.putExtra("name", et_name.getText().toString().trim());
                                startActivity(mIntent);
                                finish();

//                            } else
//                                showToast(Constant.showErrorMsg(result.getInt("code"), CreateBabySpaceActivity.this));

                        } catch (Exception e) {
                            e.printStackTrace();
                            showToast(Constant.NODATA);
                        }

                    }
                }, null);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mApplication.cancelPendingRequests(getClass().getName());
    }
}
